package com.niiwoo.civet.trade.listener;

import java.io.IOException;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.trade.constant.TradeMqConstant;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyMsgDTO;
import com.niiwoo.civet.trade.service.local.newRefund.refundDeal.core.RefundNotifyRecordHandleService;
import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;

/**
 * 还款通知监听器
 *
 * @author kevin.jia
 * @create 2018年5月14日 下午5:56:12
 */
@Slf4j
@Component
public class RefundNotifyListener {

    @Autowired
    private RefundNotifyRecordHandleService refundNotifyRecordHandleService;

    @RabbitListener(queues = { TradeMqConstant.REFUND_NOTIFY_HANDLE_QUEUE })
    public void handleNotify(RefundNotifyMsgDTO refundNotifyMsgDTO, Message message, Channel channel) throws IOException {
        log.info("还款消息处理流程MQ通知，消息内容:{}", JSON.toJSONString(refundNotifyMsgDTO));
        try{
        	refundNotifyRecordHandleService.handleNotify(refundNotifyMsgDTO);
        } catch (Exception e){
            log.error("还款消息处理流程MQ通知异常，异常原因：{}，请求参数：{}", e.getMessage(), JSON.toJSONString(refundNotifyMsgDTO), e);
        } finally {
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}